
<template>
  <!-- <h1>hello</h1>-->
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>

import resize from '@/views/dashboard/mixins/resize'
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme

import { getReturnsInfo } from "@/api/returns/returns";

const animationDuration = 6000

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },

  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      getReturnsInfo().then(response => {
        let names=[];//退货商品名称
        let values=[];//退货商品数量
        console.log(response.data);
        for(let i=0;i<response.data.length;i++){
          names.push(response.data[i].name);
          values.push(response.data[i].number);
        }
        this.chart.setOption({
          title:{
            text:"商品退货数量统计",
            left:'center'
          },
          tooltip: {
            trigger: 'axis',
            axisPointer: { // 坐标轴指示器，坐标轴触发有效
              type: 'line' // 默认为直线，可选为：'line' | 'shadow'
            }
          },
          grid: {
            top: 10,
            left: '2%',
            right: '2%',
            bottom: '3%',
            containLabel: true
          },
          xAxis: [{
            type: 'category',
            data: names,
            axisTick: {
              alignWithLabel: true
            },
            axisLabel: {
              interval: 0,//代表显示所有x轴标签显示
              rotate: 45
            }
          }],
          yAxis: [{
            type: 'value',
            axisTick: {
              show: false
            }
          }],
          dataZoom: [{ // 添加滚动条
            type: 'inside', // 内置模式
            start: 0,
            end: 100
          }, {
            show: true, // 显示工具栏
            start: 0,
            end: 100
          }],
          series: [{
            name: '退货数量',
            type: 'bar',
            stack: 'vistors',
            barWidth: '60%',
            data: values,
            animationDuration
          }]
        })
      })
    }
  }
}
</script>
<style>
</style>
